//
//  CXCycleTabView.m
//  HuoGameBox
//
//  Created by 曹想 on 2018/12/28.
//  Copyright © 2018 huosdk. All rights reserved.
//

#import "CXCycleTabView.h"
#import "CXCycleCollectionCell.h"
@interface CXCycleTabView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView * collectionView;

@property (nonatomic,strong) UICollectionViewFlowLayout * layout;

@end

@implementation CXCycleTabView

{
    float _oldPoint;
    NSInteger _dragDirection;
}


+ (instancetype)cycleScrollViewWithFrame:(CGRect)frame{
    CXCycleTabView * tab = [[CXCycleTabView alloc]initWithFrame:frame];
    return tab;
}

-(void)setItemSpace:(CGFloat)itemSpace{
    _itemSpace = itemSpace;
    self.layout.minimumLineSpacing = itemSpace;
}

-(void)setItemWidth:(CGFloat)itemWidth{
    _itemWidth = itemWidth;
    self.layout.itemSize = CGSizeMake(itemWidth, self.bounds.size.height);
}


-(void)setCollectionData:(NSArray *)collectionData{
    
    if (collectionData.count > 0) {
        //NSLog(@"%@",self.collectionView.contentSize);
        [self.collectionView reloadData];
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark -- 懒加载
-(UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _layout;
}


-(UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout: self.layout];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.userInteractionEnabled = YES;
        //_collectionView.contentSize = CGSizeMake(KScreenWidth*5, self.height);
        [_collectionView registerClass:[CXCycleCollectionCell class] forCellWithReuseIdentifier:@"CXCycleCollectionCell"];
    }
    return _collectionView;
}


#pragma mark --collectionViewDelegate UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CXCycleCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXCycleCollectionCell" forIndexPath:indexPath];
    
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _oldPoint = scrollView.contentOffset.x;
    
}

//手离开屏幕的时候
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    //如果是向右滑或者滑动距离大于item的一半，则像右移动一个item+space的距离，反之向左
    float currentPoint = scrollView.contentOffset.x;
    float moveWidth = currentPoint-_oldPoint;
    int shouldPage = moveWidth/(self.itemWidth/2);
    if (velocity.x>0 || shouldPage > 0) {
        _dragDirection = 1;//滑动方向
    }else if (velocity.x<0 || shouldPage < 0){
        _dragDirection = -1;
    }else{
        _dragDirection = 0;
    }
}

- (void)scrollViewWillBeginDecelerating: (UIScrollView *)scrollView{
    //松开手指滑动开始减速的时候，设置滑动动画
    NSInteger currentIndex = (_oldPoint + (self.itemWidth + self.itemSpace) * 0.5) / (self.itemSpace + self.itemWidth);
    if (currentIndex + _dragDirection >= 4) {
        return;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:currentIndex + _dragDirection inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}






@end
